<template>
	<view>
		<view class="">
			<view class="py10 px15 bg-fff mt10">
				<u-input placeholder="请输入姓名"  border="none" fontSize="30" v-model="addressData.truename"
					type="text" maxlength="15">
					<view class="c-333 fs14 dis-flex fwb" slot="prefix" style="width: 180rpx;">姓名</view>
				</u-input>
			</view>
			<view class="py10 px15 bg-fff mt10">
				<u-input placeholder="请输入手机号" border="none" fontSize="30" v-model="addressData.mobile"
					type="number" maxlength="11">
					<view class="c-333 fs14 dis-flex fwb" slot="prefix" style="width: 180rpx;">手机号码</view>
				</u-input>
			</view>
			
			<view class="py10 px15 bg-fff mt10 pos-rel" @tap="openAddres">
				<u-input placeholder="省、市、县、区" :readonly="true" border="none" fontSize="30" v-model="addressData.region" suffixIcon="arrow-down"
					type="text" :suffixIconStyle="{'font-size':'35rpx'}">
					<view class="c-333 fs14 dis-flex fwb" slot="prefix" style="width: 180rpx;">所在城市</view>
				</u-input>
				<view class="pos-abs wd100" style="left: 0;top: 0;height: 80rpx;"></view>
			</view>
			<view class="py10 px15 bg-fff mt10 dis-flex">
				<view class="fwb c-333 fs14" style="width: 180rpx">
					详细地址
				</view>
				<view class="flex fs15" style="margin-top: -20rpx;">
					<u-textarea :placeholderStyle="{'font-size':'30rpx','color':'#C0C4CC'}" v-model="addressData.address" border="none" confirmType="done" placeholder="请输入详细地址" maxlength="50" :height="150"></u-textarea>
				</view>
			</view>
			<!-- <view class="py10 px15 bg-fff mt10">
				<u-input placeholder="请输入详细地址" border="none" fontSize="30" v-model="addressData.address" :clearable="true"
					type="text" maxlength="50">
					<view class="c-333 fs14 dis-flex fwb" slot="prefix" style="width: 180rpx;">详细地址</view>
				</u-input>
			</view> -->
			
			<view class="py10 px15 mt10 dis-flex flex-align-center flex-between">
				<view class="fs14 c-333 fwb">
					设为默认地址
				</view>
				<view class="right">
					<switch :checked="addressData.status==1" color="#5B769F" @change="switchChange" />
				</view>
			</view>
		</view>
		<view class="center mt20">
			<button class="btn-submit bg-common c-fff" :loading="submitLoading" @tap="formSubmit">确定</button>
		</view>
	</view>
</template>

<script>
	import validate from "../../common/thformvalidate";
	export default {
		data() {
			return {
				addressData: {
					truename: '',
					mobile: '',
					region: '',
					address: '',
					// province: '',
					// city:'',
					// district:'',
					// town:'',
					status: 1,
				},
				manageType:'0',//1编辑收货地址  0新增收货地址
				submitLoading:false,
			}
		},
		onLoad(option){
			this.manageType = option.type||'0';
			let title = (this.manageType==='1')?'编辑地址':'添加地址';
			uni.setNavigationBarTitle({title});
			if(this.manageType==='1'){//读取默认数据
				this.addressData = JSON.parse(decodeURIComponent(option.data));
				this.addressData.region = this.addressData.region + '/' + this.addressData.city + '/' + this.addressData.xian;
			}
		},
		methods: {
			
			switchChange(e){
				this.addressData.status = e.detail.value?1:0;
			},
			
			openAddres() {
				this.$api.openWindow('/pages/center/selectArea');
			},
			
			//选择地址会调
			areaConfirm(data) {
				this.addressData.region = data.name;
				this.addressData.province = data.province;
				this.addressData.city = data.city || '';
				this.addressData.district = data.district || '';
				this.addressData.town = data.town || '';
			},
			
			formSubmit(e){//提交
				//进行表单检查
				let {
					truename,mobile,region,address,status
				} = this.addressData
				var formData = {
					truename,mobile,region,address,status
				};
				let formRule = [//表单规则
					{name:"truename", notnull:true, checkType : "",  title:"姓名"},
					{name:"mobile", notnull:true, checkType : "phone",  title:"手机号码"},
					{name:"region", notnull:true, checkType : "",  title:"所在城市"},
					{name:"address", notnull:true, checkType : "",  title:"详细地址"},
				];
				if(!validate.check(formData, formRule)) return;
				var ajaxUrl = (this.manageType==='1')?this.$config.UrlEditAddress:this.$config.UrlAddAddress;
				if(this.manageType==='1'){
					formData.id = this.addressData.id;
				}
				this.submitLoading = true;
				this.$http.post(ajaxUrl,formData,{
					header:{
						'content-type': "application/json",
					}
				}).then((res)=>{
					if(res.code == 1){
						this.$api.alert(res.msg,()=>{
							this.$api.opener().refreshList();
							this.$api.back();
						})
					}else this.$api.toast(res.msg);
				}).finally((res)=>{
					this.submitLoading = false;
				})
			},
		}
	}
</script>

<style>
	
</style>
